﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.WordAction
{
    public partial class WagesApprovedControl : UserControl
    {
        private readonly WordActionService.WordActionClient _client = new WordActionService.WordActionClient();
        public WagesApprovedControl()
        {
            InitializeComponent();
            _client.GetWagesApprovedCompleted += new EventHandler<WordActionService.GetWagesApprovedCompletedEventArgs>(_client_GetWagesApprovedCompleted);
            _client.GetWagesApprovedAsync("1=1");
            _client.DeleteWagesApprovedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_DeleteWagesApprovedCompleted);
        }

        void _client_DeleteWagesApprovedCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
                _client.GetWagesApprovedAsync("1=1");
            }
        }

        void _client_GetWagesApprovedCompleted(object sender, WordActionService.GetWagesApprovedCompletedEventArgs e)
        {
            dgList.ItemsSource = null;
            dgList.ItemsSource = e.Result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            WagesApprovedWindow wind = new WagesApprovedWindow(0);
            wind.Show();
            wind.Closed += (sender1, e1) =>
            {
                if (wind.DialogResult == true)
                    _client.GetWagesApprovedAsync("1=1");
            };
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (dgList.SelectedItem == null)
            {
                return;
            }
            var entity = dgList.SelectedItem as WordActionService.Word_WagesApproved;
            if (entity != null)
            {
                WagesApprovedWindow wind = new WagesApprovedWindow((int)entity.ID);
                wind.Show();
                wind.Closed += (sender1, e1) =>
                {
                    if (wind.DialogResult == true)
                        _client.GetWagesApprovedAsync("1=1");
                };
            }
        }

        private void ImageButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (dgList.SelectedItem == null)
            {
                return;
            }
            var entity = dgList.SelectedItem as WordActionService.Word_WagesApproved;
            if (entity != null)
            {

                _client.ExportWagesApproveAsync((int)entity.ID);
                _client.ExportWagesApproveCompleted += (sender1, e1) =>
                {
                    if (e1.Error == null)
                    {
                        if (!string.IsNullOrEmpty(e1.Result))
                        {
                            try
                            {
                                string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e1.Result, UriKind.RelativeOrAbsolute));
                            }
                            catch (Exception ex)
                            {
                                SilverlightMessageBoxLibrary.Message.ErrorMessage("导出失败，可能原因为\n" + e1.Result);
                            }
                        }
                       
                    }
                };

            }

        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (dgList.SelectedItem == null)
            {
                return;
            }
            var entity = dgList.SelectedItem as WordActionService.Word_WagesApproved;
            _client.DeleteWagesApprovedAsync(entity.ID);
        }
    }
}
